package com.ev.apis.controller.asl;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.k3cloudBusiness.service.K3SalesStoreHouseService;
import com.ev.k3cloudBusiness.vo.SalesStorehouseByClientVo;
import com.ev.k3cloudBusiness.vo.SalesStorehouseByProductVo;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售出库报表API
 * @author 姚家乐
 * @since 2021/8/3 13:06
 */
@Deprecated
@Api(value = "/", tags = "销售出库报表API")
@RestController
public class SalesStoreHouseController {

    @Autowired
    private K3SalesStoreHouseService service;

    /**
     * 查看销售出库(根据物料)报表
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/SalesStoreHouseByProduct/query", method = RequestMethod.POST, apiTitle = "查看销售出库(根据物料)报表")
    @ApiOperation("查看销售出库(根据物料)报表")
    public R queryByProduct(
            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
            @ApiParam(value = "年份", required = true) @RequestParam(value = "yearDate", defaultValue = "") String yearDate
    ) {
        Map<String,Object> param = new HashMap<>();
        param.put("pageno",pageno);
        param.put("pagesize",pagesize);
        param.put("yearDate",yearDate);
        return service.salesStoreHouseByProductSync(param);
    }

    /**
     * 查看销售出库(根据物料)报表导出
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/SalesStoreHouseByProduct/export", method = RequestMethod.GET, apiTitle = "查看销售出库(根据物料)报表导出")
    @ApiOperation("查看销售出库(根据物料)报表导出")
    public void exportProduct(
            @ApiParam(value = "年份", required = true) @RequestParam(value = "yearDate", defaultValue = "") String yearDate,
            HttpServletRequest request, HttpServletResponse response,
            ModelMap modelMap) {
        Map<String,Object> param = new HashMap<>();
        param.put("yearDate",yearDate);
        Map<String,Object> o = service.salesStoreHouseByProductSync(param);
        List<SalesStorehouseByProductVo> byProductVos = (List<SalesStorehouseByProductVo>) o.get("byProduct");

        ClassPathResource classPathResource = new ClassPathResource(
                "poi/sales_store_house_by_product.xlsx");
        Map<String, Object> map = Maps.newHashMap();
        map.put("list",byProductVos);



        TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
        modelMap.put(TemplateExcelConstants.FILE_NAME, "季度产品销售分析表");
        modelMap.put(TemplateExcelConstants.PARAMS, result);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

    /**
     * 查看销售出库(根据客户)报表
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/SalesStoreHouseByClient/query", method = RequestMethod.POST, apiTitle = "查看销售出库(根据客户)报表")
    @ApiOperation("查看销售出库(根据客户)报表")
    public R queryByClient(
            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "20") int pagesize,
            @ApiParam(value = "年份", required = true) @RequestParam(value = "yearDate", defaultValue = "") String yearDate
    ) {
        Map<String,Object> param = new HashMap<>();
        param.put("pageno",pageno);
        param.put("pagesize",pagesize);
        param.put("yearDate",yearDate);
        return service.salesStoreHouseByClientSync(param);
    }

    /**
     * 查看销售出库(根据客户)报表导出
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/SalesStoreHouseByClient/export", method = RequestMethod.GET, apiTitle = "查看销售出库(根据客户)报表导出")
    @ApiOperation("查看销售出库(根据客户)报表导出")
    public void exportClient(
            @ApiParam(value = "年份", required = true) @RequestParam(value = "yearDate", defaultValue = "") String yearDate,
            HttpServletRequest request, HttpServletResponse response,
            ModelMap modelMap) {
        Map<String,Object> param = new HashMap<>();
        param.put("yearDate",yearDate);
        Map<String,Object> o = service.salesStoreHouseByClientSync(param);
        List<SalesStorehouseByClientVo> byProductVos = (List<SalesStorehouseByClientVo>) o.get("byClient");

        ClassPathResource classPathResource = new ClassPathResource(
                "poi/sales_store_house_by_client.xlsx");
        Map<String, Object> map = Maps.newHashMap();
        map.put("list",byProductVos);



        TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
        modelMap.put(TemplateExcelConstants.FILE_NAME, "客户全季度销售额数据统计表");
        modelMap.put(TemplateExcelConstants.PARAMS, result);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }
}
